
#pragma once

#include "Defines.h"
#include "NetworkPacket.h"

#include <irrlicht.h>
// BOOST INCLUDES
#include <boost/thread/thread.hpp>
#include <boost/array.hpp>

#include <boost/asio.hpp>
#include <iostream>
#include "Serializer.h"
#include "MovingObject.h"

using boost::asio::ip::udp;

struct ClientAddress 
{
	std::string ip;
	unsigned short int port;
};

//typedef std::pair<NetworkPacket, ClientAddress> received_packet; 

class SocketCommunicator
{
	private:
		boost::asio::io_service ioService;
		udp::socket *socket;

		udp::endpoint serverEndpoint;
		udp::endpoint clientEndpoint;
		
		std::string recvPacket;
		boost::array<char, 1024> recvBuffer;

		boost::shared_ptr<boost::thread> thread;
		
	//	received_vector receivedPackets;

		std::string serverIP;
		unsigned int port;
	public:
		SocketCommunicator();
		void start();
		void stop();

		void startCommunication();
		boost::asio::io_service* getIOService();

		void sendSync(const std::string& message);
		bool sendAsync(ClientAddress* passedClientAddress, const std::string& message);
		
		void asyncReceive();
		void syncReceive();
	private:
		void handleReceive(const boost::system::error_code& error, std::size_t bytes_transferred);
		void handleSend(std::string* message, const boost::system::error_code& error, std::size_t bytes_transferred);
};
